//@author A0096950U
#include "Utilities.h"

const string Utilities::STRING_EMPTY = "";
const string Utilities::STRING_WHITESPACE = " ";

const string Utilities::STRING_INVALID_MONTH = "Invalid Month";
const string Utilities::STRING_DEFAULT_DESCRIPTION = "description";
const string Utilities::STRING_DEFAULT_DATE = "date";
const string Utilities::STRING_DEFAULT_START_TIME = "start time";
const string Utilities::STRING_DEFAULT_END_TIME = "end time";
const string Utilities::STRING_DEFAULT_TAG = "untagged";

const string Utilities::STRING_DEFAULT_START_TIME_NUMERICAL = "0000";
const string Utilities::STRING_DEFAULT_END_TIME_NUMERICAL = "2359";

const string Utilities::STRING_ADD = "add";
const string Utilities::STRING_DELETE = "delete";
const string Utilities::STRING_MODIFY = "modify";
const string Utilities::STRING_TO = "to";
const string Utilities::STRING_DONE = "done";
const string Utilities::STRING_SEARCH = "search";
const string Utilities::STRING_TODAY = "today";
const string Utilities::STRING_UNDO = "undo";
const string Utilities::STRING_EXIT = "exit";
const string Utilities::STRING_ALL = "all";

const string Utilities::STRING_TOMORROW = "tomorrow";

const string Utilities::STRING_MON = "Mon";
const string Utilities::STRING_TUE = "Tue";
const string Utilities::STRING_WED = "Wed";
const string Utilities::STRING_THU = "Thu";
const string Utilities::STRING_FRI = "Fri";
const string Utilities::STRING_SAT = "Sat";
const string Utilities::STRING_SUN = "Sun";

const string Utilities::STRING_BLOCK_OPEN = "[";
const string Utilities::STRING_BLOCK_CLOSE = "]";

const string Utilities::STRING_AM = "AM";
const string Utilities::STRING_PM = "PM";
const string Utilities::STRING_MN = "MN";
const string Utilities::STRING_NN = "NN";

const string Utilities::STRING_JAN = "Jan";
const string Utilities::STRING_FEB = "Feb";
const string Utilities::STRING_MAR = "Mar";
const string Utilities::STRING_APR = "Apr";
const string Utilities::STRING_MAY = "May";
const string Utilities::STRING_JUN = "Jun";
const string Utilities::STRING_JUL = "Jul";
const string Utilities::STRING_AUG = "Aug";
const string Utilities::STRING_SEP = "Sep";
const string Utilities::STRING_OCT = "Oct";
const string Utilities::STRING_NOV = "Nov";
const string Utilities::STRING_DEC = "Dec";

void Utilities::trimString(string& inputString) {

	unsigned int start = inputString.find_first_not_of(STRING_WHITESPACE);

	if(start != string::npos) {
		inputString = inputString.substr(start);
	}
	else {
		inputString = STRING_EMPTY;
	}
}

string Utilities::extractFirstWord(string& inputString) {

	Utilities::trimString(inputString);

	unsigned int space = inputString.find_first_of(STRING_WHITESPACE);

	string firstWord(inputString, 0, space);
	inputString = inputString.substr(space+1);

	Utilities::trimString(inputString);

	return firstWord;
}

//@author A0097466M
void Utilities::toLowerCase(string& inputString) {
	for(unsigned i = 0; i < inputString.size(); i++) {
		if((inputString[i] >= 'A') && (inputString[i] <= 'Z')) {
			inputString[i] = 'a' + (inputString[i] - 'A');
		}
	}
}

void Utilities::toUpperCase(string&inputString) {
	for(unsigned i = 0; i < inputString.size(); i++) {
		if((inputString[i] >= 'a') && (inputString[i] <= 'z')) {
			inputString[i] = 'A' + (inputString[i] - 'a');
		}
	}
}

void Utilities::toStandardCase(string& inputString){
	if((inputString[0] >= 'a') && (inputString[0] <= 'z')){
		inputString[0] = 'A' + (inputString[0] - 'a');
	}

	for(unsigned i=1;i<inputString.size();i++){
		if((inputString[i] >= 'A') && (inputString[i] <= 'Z')){
			inputString[i] = 'a' + (inputString[i] - 'A');
		}
	}
}

string Utilities::monthInWords(int monthInNumbers) {
	assert(monthInNumbers >= 1 && monthInNumbers <= 12);

	string toReturn;

	switch (monthInNumbers) {
	case 1:
		toReturn = STRING_JAN;
		break;
	case 2:
		toReturn = STRING_FEB;
		break;
	case 3:
		toReturn = STRING_MAR;
		break;
	case 4:
		toReturn = STRING_APR;
		break;
	case 5:
		toReturn = STRING_MAY;
		break;
	case 6:
		toReturn = STRING_JUN;
		break;
	case 7:
		toReturn = STRING_JUL;
		break;
	case 8:
		toReturn = STRING_AUG;
		break;
	case 9: 
		toReturn = STRING_SEP;
		break;
	case 10:
		toReturn = STRING_OCT;
		break;
	case 11:
		toReturn = STRING_NOV;
		break;
	case 12:
		toReturn = STRING_DEC;
		break;
	}

	return toReturn;
}

//@author A0105511Y
string Utilities::convertTo12Hr(int time) {

	string timeToReturn;

	if(time >= 1200) {
		timeToReturn = timeInPM(time);
	}
	else {
		timeToReturn = timeInAM(time);
	}

	return timeToReturn;
}

string Utilities::timeInAM(int time) {
	assert(time >= 0 && time <= 1159);

	string timeToReturn;

	if(time == 0) {
		timeToReturn = "0000" + STRING_WHITESPACE + STRING_AM;
	} else if (time > 0 && time < 59) {
		timeToReturn = "00" + to_string(time) + STRING_WHITESPACE + STRING_AM;
	} else if(time < 1000) {
		timeToReturn = "0" + to_string(time) + STRING_WHITESPACE + STRING_AM;
	} else {
		timeToReturn = to_string(time) + STRING_WHITESPACE + STRING_AM;
	}

	return timeToReturn;
}

string Utilities::timeInPM(int time) {
	assert(time >= 1200 && time <= 2359);

	string timeToReturn;

	if(time <= 1259) {
		timeToReturn = to_string(time) + STRING_WHITESPACE + STRING_PM;
	} else {
		time = time - 1200;
		if(time < 1000 && time > 59) {
			timeToReturn = "0" + to_string(time) + STRING_WHITESPACE + STRING_PM;
		} else {
			timeToReturn = to_string(time) + STRING_WHITESPACE + STRING_PM;
		}
	}

	return timeToReturn;
}